package log

import (
	"cm2"
	"cm2/util"
	"path"
	"time"

	"github.com/kataras/golog"
	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
)

// NewRotateLog 创建按时间滚动的日志
func NewRotateLog(
	logRoot string,
	logName string,
	logLevel string,
	maxAge time.Duration,
	rotationTime time.Duration,
) *golog.Logger {
	l := golog.New()

	if logName == "" {
		logName = "log"
	}
	if logLevel == "" {
		logLevel = "info"
	}

	if util.GetGOENV() != cm2.DEV {
		util.Mkdir(logRoot)
		baseLogName := path.Join(logRoot, logName)
		writer, _ := rotatelogs.New(
			baseLogName+"%Y-%m-%d"+".log",
			// rotatelogs.WithLinkName(basePath),
			rotatelogs.WithMaxAge(maxAge),
			rotatelogs.WithRotationTime(rotationTime),
		)

		l.AddOutput(writer)
	}

	l.SetLevel(logLevel)

	return l
}
